﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tilver.Data;
using Weixin.MP.DAL.WeixinMP;

namespace Weixin.MP.BLL.Users
{
    public class UsersBO
    {
        private UsersMO _mo;
        private OperateLogMO _operateLogMo;
        public UsersBO()
        {
            _mo = new UsersMO();
            _operateLogMo = new OperateLogMO();
        }

        #region 查询
        public UsersEO GetByUserId(string userId, TransactionManager tm = null)
        {
            return _mo.SelectByPK(userId, tm);
        }
        public UsersEO GetUsers(string userId, string password)
        {
            var sqlWhere = new StringBuilder();
            sqlWhere.Append("[UserId]=@UserId and [Password]=@Password");
            var paras = new List<DbParameter>();
            {
                var p = new SqlParameter("@UserId", SqlDbType.VarChar, 20);
                p.Value = userId;
                paras.Add(p);
            }
            {
                var p = new SqlParameter("@Password", SqlDbType.VarChar, 50);
                p.Value = password;
                paras.Add(p);
            }
            return _mo.Select(sqlWhere.ToString(), 0, string.Empty, paras).FirstOrDefault();
        }
        public List<UsersEO> GetUsers(int pageSize, int pageIndex, ref long recordCount)
        {
            var sqlWhere = new StringBuilder();
            sqlWhere.Append(" 1=1 ");
            var paras = new List<DbParameter>();
            return _mo.SelectPager(sqlWhere.ToString(), " [UserId] DESC ", paras, pageSize, pageIndex, ref recordCount);
        }

        #endregion
        #region 修改
        /// <summary>
        /// 更新最后一次登录时间
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="loginTime"></param>
        /// <param name="tm"></param>
        public void UpdateLoginTime(string userId, DateTime loginTime, TransactionManager tm = null)
        {
            _mo.UpdateLoginTime(userId, loginTime, tm);
        }
        public void EditUsers(UsersEO eo, TransactionManager tm = null)
        {
            _mo.Update(eo, tm);
        }
        #endregion
        #region 添加
        public void AddOperateLog(OperateLogEO eo, TransactionManager tm = null)
        {
            _operateLogMo.Insert(eo, tm);
        }
        public void AddUsers(UsersEO eo, TransactionManager tm = null)
        {
            _mo.Insert(eo, tm);
        }
        #endregion
        #region 删除

        public void DelUsers(string userId, TransactionManager tm = null)
        {
            _mo.DeleteByPK(userId, tm);
        }
        #endregion
    }
}
